package com.action;

import com.entity.Cinema;
import com.entity.Hall;
import com.service.CinemaService;
import com.service.HallService;
import com.util.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

//影厅管理
@Controller
// 设置路径
@RequestMapping(value = "/hall", produces = "text/plain;charset=utf-8")
public class HallAction extends BaseAction {

    @Autowired
    private HallService hallService;
    @Autowired
    private CinemaService cinemaService;
    
    // 准备添加数据
    @RequestMapping("createHall.action")
    public String createHall() {
        List<Cinema> allCinema = cinemaService.getAllCinema();
        this.getRequest().setAttribute("cinemaList", allCinema);
        return "admin/addhall";
    }

    // 添加数据
    @RequestMapping("addHall.action")
    public String addHall(Hall hall) {
        this.hallService.insertHall(hall);
        return "redirect:/hall/getAllHall.action";
    }

    // 通过主键删除数据
    @RequestMapping("deleteHall.action")
    public String deleteHall(String id) {
        this.hallService.deleteHall(id);
        return "redirect:/hall/getAllHall.action";
    }

    // 批量删除数据
    @RequestMapping("deleteHallByIds.action")
    public String deleteHallByIds() {
        String[] ids = this.getRequest().getParameterValues("hallid");
        for (String hallid : ids) {
            this.hallService.deleteHall(hallid);
        }
        return "redirect:/hall/getAllHall.action";
    }

    // 更新数据
    @RequestMapping("updateHall.action")
    public String updateHall(Hall hall) {
        hallService.updateHall(hall);
        return "redirect:/hall/getAllHall.action";
    }

    // 显示全部数据
    @RequestMapping("getAllHall.action")
    public String getAllHall(String number) {
        List<Hall> hallList = this.hallService.getAllHall();
        PageHelper.getPage(hallList, "hall", null, null, 10, number, this.getRequest(), null);
        return "admin/listhall";
    }

   /* // 按条件查询数据 (模糊查询)
    @RequestMapping("queryHallByCond.action")
    public String queryHallByCond(String cond, String name, String number) {

        Hall hall = new Hall();
        if (cond != null) {
            if ("hallid".equals(cond)) {
                hall.setHallid(name);
            }
            if ("hallname".equals(cond)) {
                hall.setHallname(name);
            }
        }

        List<String> nameList = new ArrayList<String>();
        List<String> valueList = new ArrayList<String>();
        nameList.add(cond);
        valueList.add(name);
        PageHelper.getPage(this.hallService.getHallById(hall), "hall", nameList, valueList, 10, number, this.getRequest(), "query");
        name = null;
        cond = null;
        return "admin/queryhall";

    }*/

    // 按主键查询数据
    @RequestMapping("getHallById.action")
    public String getHallById(String id) {

        Hall hall = this.hallService.getHallById(id);
        this.getRequest().setAttribute("hall", hall);
        List<Cinema> cinemas = cinemaService.getAllCinema();
        this.getRequest().setAttribute("cinemaList", cinemas);
        return "admin/edithall";

    }

}